package com.yike.product.controller;

import com.yike.product.param.GoodsUnitParam;
import com.yike.product.service.GoodsUnitService;
import com.yike.product.vo.GoodsUnitVo;
import lombok.extern.slf4j.Slf4j;
import com.yike.product.param.GoodsUnitPageParam;
import com.yike.common.base.BaseController;
import com.yike.common.api.ApiResult;
import com.yike.common.pagination.Paging;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.enums.OperationLogType;
import com.yike.common.api.Add;
import com.yike.common.api.Update;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品单位 控制器
 *
 * @author Sakura
 * @since 2024-10-18
 */
@Slf4j
@RestController
@RequestMapping("/platform/goodsUnit")
@Module("product")
@Api(value = "商品单位API", tags = {"平台商品单位"})
public class GoodsUnitController extends BaseController {

    @Autowired
    private GoodsUnitService goodsUnitService;

    /**
     * 添加商品单位
     */
    @PostMapping("/add")
    @OperationLog(name = "添加商品单位", type = OperationLogType.ADD)
    @ApiOperation(value = "添加商品单位", response = ApiResult.class)
    public ApiResult<Boolean> addGoodsUnit(@Validated(Add.class) @RequestBody GoodsUnitParam goodsUnitParam) throws Exception {
        boolean flag = goodsUnitService.saveGoodsUnit(goodsUnitParam);
        return ApiResult.result(flag);
    }

    /**
     * 修改商品单位
     */
    @PostMapping("/update")
    @OperationLog(name = "修改商品单位", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改商品单位", response = ApiResult.class)
    public ApiResult<Boolean> updateGoodsUnit(@Validated(Update.class) @RequestBody GoodsUnitParam goodsUnitParam) throws Exception {
        boolean flag = goodsUnitService.updateGoodsUnit(goodsUnitParam);
        return ApiResult.result(flag);
    }

    /**
     * 删除商品单位
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除商品单位", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除商品单位", response = ApiResult.class)
    public ApiResult<Boolean> deleteGoodsUnit(@PathVariable("id") Long id) throws Exception {
        boolean flag = goodsUnitService.deleteGoodsUnit(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取商品单位详情
     */
    @GetMapping("/info/{id}")
    @OperationLog(name = "商品单位详情", type = OperationLogType.INFO)
    @ApiOperation(value = "商品单位详情", response = GoodsUnitVo.class)
    public ApiResult<GoodsUnitVo> getGoodsUnit(@PathVariable("id") Long id) throws Exception {
        GoodsUnitVo goodsUnitVo = goodsUnitService.getGoodsUnit(id);
        return ApiResult.ok(goodsUnitVo);
    }

    /**
     * 商品单位分页列表
     */
    @PostMapping("/getPageList")
    //@OperationLog(name = "商品单位分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "商品单位分页列表", response = GoodsUnitVo.class)
    public ApiResult<Paging<GoodsUnitVo>> getGoodsUnitPageList(@Validated @RequestBody GoodsUnitPageParam goodsUnitPageParam) throws Exception {
        Paging<GoodsUnitVo> paging = goodsUnitService.getGoodsUnitPageList(goodsUnitPageParam);
        return ApiResult.ok(paging);
    }

}

